﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ITAP.ShprossesBuilder.Modelling.Components.Constraints
{
    [Serializable]
    public class RemoveUnnecessaryInnerSnapOnCapComponentConstraint : IConstraint
    {
        private bool enabled = true;

        public RemoveUnnecessaryInnerSnapOnCapComponentConstraint()
        {
        }

        #region IConstraint Members

        public bool Check(IComponent component)
        {
            return false;
        }

        public void Fix(IComponent component)
        {
            if (!enabled)
                return;

            List<IComponent> componentsToRemove = new List<IComponent>();
            foreach (IComponent innerComponent in component.InnerComponents)
                if (innerComponent is SnapOnCap)
                    componentsToRemove.Add(innerComponent);
            if (componentsToRemove.Count == 1)
                return;
            else
                for(int i = 0; i < componentsToRemove.Count - 1; i++)
                    component.RemoveInnerComponent(componentsToRemove[i]);
                
        }

        public bool Enabled
        {
            get
            {
                return enabled;
            }
            set
            {
                enabled = value;
            }
        }

        public ConstraintAction Action
        {
            get { return ConstraintAction.Fix; }
        }

        #endregion

        #region ICloneable Members

        public object Clone()
        {
            RemoveUnnecessaryInnerSnapOnCapComponentConstraint clone = new RemoveUnnecessaryInnerSnapOnCapComponentConstraint();
            clone.Enabled = this.enabled;
            return clone;
        }

        #endregion
    }
}
